package com.ruoyi.employee.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.employee.util.OSSUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/oss")
public class OssController {
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        // 截取后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 生成新的文件名（唯一性）
        String fileName = UUID.randomUUID().toString() + suffix;
        // 上传文件
        String url = OSSUtil.upload(fileName, file.getInputStream());
        return AjaxResult.success((Object) url);
    }
    @GetMapping("/download")
    public void download2(String fileName, HttpServletResponse response) throws IOException {
        /*设置文档的保存形式*/
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        response.setContentType("application/octet-stream");
        ServletOutputStream out = response.getOutputStream();
        OSSUtil.download(fileName,out);
    }
}
